<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" :model="userForm" label-width="220px" :rules="rules">
          <!-- 姓名 部门 -->
          <el-row>
            <!-- 姓名 -->
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="userForm.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
            <!-- 工号 -->
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>

          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  v-model="userForm.mobile"
                  :disabled="!!$route.params.id"
                  size="mini"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <ImportTree v-model="userForm.departmentId" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="userForm.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="外包" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="userForm.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="userForm.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <ImageUpload v-model="userForm.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>

          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="btnOK">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import { addEmployeeAPI, getEmployeeAPI, updateEmployeeAPI } from '@/api'
import { ImageUpload, ImportTree } from './components'

export default {
  components: {
    ImportTree,
    ImageUpload
  },
  data() {
    return {
      userForm: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentId: 0,
        timeOfEntry: '',
        correctionTime: '',
        staffPhoto: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 1, max: 4, message: '长度在 1 到 4 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { validator: (rule, value, callback) => {
            value ? callback() : callback(new Error('请选择'))
          }, trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请输入入职时间', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请输转正时间', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (new Date(value) < new Date(this.userForm.timeOfEntry)) {
              callback(new Error('转正时间不能早于入职时间'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.$route.params.id && this.getEmployeeDetail()
    // !this.$route.params.id || this.getEmployeeDetail()
    // if (this.$route.params.id) {
    //   this.getEmployeeDetail()
    // }
  },
  methods: {
    // 更新按钮
    async btnOK() {
      await this.$refs.userForm.validate()
      if (this.$route.params.id) { // 判断是否有id 则执行编辑功能
        await updateEmployeeAPI(this.userForm)
        this.$message.success('编辑成功')
      } else {
        await addEmployeeAPI(this.userForm)
        this.$message.success('新增成功')
      }
      this.$router.push('/employee')
    },
    async getEmployeeDetail() {
      // 接收传输过来的跳转的值，并赋值给本地
      this.userForm = await getEmployeeAPI(this.$route.params.id)
    }
  }
}
</script>

  <style scoped lang="scss">
      .edit-form {
        background: #fff;
        padding: 20px;
        .inputW {
          width: 380px
        }
      }

  </style>
